﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAccess;
using EPiServer.Security;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PageTypeBuilder;
using TemplateFoundation.Configuration;
using TemplateFoundation.Core;
using TemplateFoundation.Tests.Environment;
using TemplateFoundation.Tests.PageTypes;

namespace TemplateFoundation.Tests.UnitTests.TemplateFoundation.EventHandlers
{
    [TestClass]
    public class NewsContainerDateFolderSorting
    {
        [TestInitialize]
        public void Setup()
        {
            Settings.Instance.NewsContainer = PageStructure.CreateNewsContainer("News");
        }

        [TestCleanup]
        public void Cleanup()
        {
            DataFactory.Instance.Delete(Settings.Instance.NewsContainer, true, AccessLevel.NoAccess);
            Settings.Instance.NewsContainer = null;
        }

        [TestMethod]
        public void CreateNewArticleWithDateFolderSortingEnabled()
        {
            Settings.Instance.DisableDateFolderSorting=false;

            var newArticle = DataFactory.Instance.GetDefaultPageData(
                Settings.Instance.NewsContainer,
                PageTypeResolver.Instance.GetPageTypeID(typeof (ArticlePage)).Value);

            DataFactory.Instance.Save(newArticle, SaveAction.Publish, AccessLevel.NoAccess);

            Assert.IsTrue(newArticle.ParentLink.GetPage().PageName==newArticle.StartPublish.Month.ToString(), "The article was not put in a correct folder, parent page is not named after StartPublish.Month");

            Assert.IsTrue(newArticle.ParentLink.GetPage().ParentLink.GetPage().PageName==newArticle.StartPublish.Year.ToString(), "The article was not put in a correct year folder, parent of parent page is not named after StartPublish.Year");
        }

        [TestMethod]
        public void CreateNewArticleWithDateFolderSortingDisabled()
        {
            Settings.Instance.DisableDateFolderSorting=true;

            var newArticle = DataFactory.Instance.GetDefaultPageData(
                Settings.Instance.NewsContainer,
                PageTypeResolver.Instance.GetPageTypeID(typeof(ArticlePage)).Value);

            DataFactory.Instance.Save(newArticle, SaveAction.Publish, AccessLevel.NoAccess);

            Assert.IsTrue(newArticle.ParentLink==Settings.Instance.NewsContainer);
        }
    }
}
